/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.willy.example.slice;

import com.willy.example.DemoFragment;
import com.willy.example.ListFragment;
import com.willy.example.MyPageSliderProvider;
import com.willy.example.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.PageSlider;
import ohos.agp.components.TabList;
import ohos.agp.utils.Color;
import ohos.agp.window.service.WindowManager;
import ohos.bundle.AbilityInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * ability
 *
 * @since 2021-03-26
 */
public class MainAbilitySlice extends AbilitySlice {
    private PageSlider pageslider;
    private TabList tabList;
    private DemoFragment demoFragment;
    private ListFragment listFragment;
    private MyPageSliderProvider myPageSliderProvider;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_mainlayout);
        WindowManager.getInstance().getTopWindow().get().setStatusBarColor(Color.getIntColor("#4425c1"));
        demoFragment = new DemoFragment(this);
        listFragment = new ListFragment(this);
        tabList = (TabList) findComponentById(ResourceTable.Id_tab_list);

        pageslider = (PageSlider) findComponentById(ResourceTable.Id_pageslider);
        List<Component> componentList = componentList();
        myPageSliderProvider = new MyPageSliderProvider(this, componentList);
        pageslider.setProvider(myPageSliderProvider);

        TabList.Tab tab = tabList.new Tab(this);
        tab.setText("ANIMATION DEMO");
        TabList.Tab tab2 = tabList.new Tab(getContext());
        tab2.setText("RECYCLERVIEW DEMO");
        tabList.addTab(tab);
        tabList.addTab(tab2);
        tabList.selectTabAt(0);
        tabList.addTabSelectedListener(new TabList.TabSelectedListener() {
            @Override
            public void onSelected(TabList.Tab tab) {
                pageslider.setCurrentPage(tab.getPosition());
            }

            @Override
            public void onUnselected(TabList.Tab tab) {
                // 当某个Tab从选中状态变为未选中状态时的回调
            }

            @Override
            public void onReselected(TabList.Tab tab) {
                // 当某个Tab已处于选中状态，再次被点击时的状态回调
            }
        });
        pageslider.addPageChangedListener(new PageSlider.PageChangedListener() {
            @Override
            public void onPageSliding(int i, float v, int i1) {
            }

            @Override
            public void onPageSlideStateChanged(int i) {
            }

            @Override
            public void onPageChosen(int i) {
                tabList.selectTabAt(i);
            }
        });
    }

    private List<Component> componentList() {
        List<Component> list = new ArrayList<>();
        list.add(demoFragment);
        list.add(listFragment);
        return list;
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    protected void onOrientationChanged(AbilityInfo.DisplayOrientation displayOrientation) {
        super.onOrientationChanged(displayOrientation);
    }
}
